import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:flutter/foundation.dart';
import 'services/token_service.dart';
import 'services/user_service.dart';
import 'services/theme_service.dart';
import 'services/locale_service.dart';
import 'providers/auth_provider.dart';
import 'providers/system_provider.dart';
import 'providers/product_provider.dart';
import 'providers/device_provider.dart';
import 'providers/data_provider.dart';
import 'repositories/auth_repository.dart';
import 'repositories/system_repository.dart';
import 'repositories/product_repository.dart';
import 'repositories/device_repository.dart';
import 'repositories/data_repository.dart';
import 'services/product_service.dart';
import 'services/device_service.dart';
import 'services/alert_log_service.dart';
import 'services/gb_video_service.dart';

/// 负责初始化应用所需的各种服务
class ServiceInitializer {
  /// 初始化所有服务
  static Future<void> init() async {
    try {
      debugPrint('开始初始化服务...');
      
      // 等待GetStorage初始化完成，这是一个必须的依赖
      await GetStorage.init();
      debugPrint('GetStorage初始化完成');
      
      // 按依赖顺序注册服务
      
      // 1. 基础服务，不依赖其他服务
      await _initBaseServices();
      
      // 2. Repository层，可能依赖基础服务
      await _initRepositories();
      
      // 3. 业务服务层，可能依赖Repository
      await _initBusinessServices();
      
      debugPrint('所有服务初始化完成');
    } catch (e) {
      debugPrint('服务初始化失败: $e');
      rethrow;
    }
  }
  
  /// 初始化基础服务
  static Future<void> _initBaseServices() async {
    // 主题服务
    final themeService = ThemeService();
    Get.put<ThemeService>(themeService, permanent: true);
    
    // 本地化服务
    final localeService = LocaleService();
    Get.put<LocaleService>(localeService, permanent: true);
    
    // Token服务
    final tokenService = TokenService();
    Get.put<TokenService>(tokenService, permanent: true);
    
    debugPrint('基础服务初始化完成');
  }
  
  /// 初始化Repository层
  static Future<void> _initRepositories() async {
    // 确保不重复注册
    if (!Get.isRegistered<AuthProvider>()) {
      // 认证提供者
      final authProvider = AuthProvider();
      Get.put<AuthProvider>(authProvider, permanent: true);
    }
    
    if (!Get.isRegistered<AuthRepository>()) {
      // 认证仓库
      final authRepository = AuthRepository();
      Get.put<AuthRepository>(authRepository, permanent: true);
    }
    
    if (!Get.isRegistered<SystemProvider>()) {
      // 系统提供者
      final systemProvider = SystemProvider();
      Get.put<SystemProvider>(systemProvider, permanent: true);
    }
    
    if (!Get.isRegistered<SystemRepository>()) {
      // 系统仓库
      final systemRepository = SystemRepository();
      Get.put<SystemRepository>(systemRepository, permanent: true);
    }
    
    if (!Get.isRegistered<ProductProvider>()) {
      // 产品提供者
      final productProvider = ProductProvider();
      Get.put<ProductProvider>(productProvider, permanent: true);
    }
    
    if (!Get.isRegistered<ProductRepository>()) {
      // 产品仓库
      final productRepository = ProductRepository();
      Get.put<ProductRepository>(productRepository, permanent: true);
    }
    
    if (!Get.isRegistered<DeviceProvider>()) {
      // 设备提供者
      final deviceProvider = DeviceProvider();
      Get.put<DeviceProvider>(deviceProvider, permanent: true);
    }
    
    if (!Get.isRegistered<DeviceRepository>()) {
      // 设备仓库
      final deviceRepository = DeviceRepository();
      Get.put<DeviceRepository>(deviceRepository, permanent: true);
    }
    
    if (!Get.isRegistered<DataProvider>()) {
      // 数据提供者
      final dataProvider = DataProvider();
      Get.put<DataProvider>(dataProvider, permanent: true);
    }
    
    if (!Get.isRegistered<DataRepository>()) {
      // 数据仓库
      final dataRepository = DataRepository();
      Get.put<DataRepository>(dataRepository, permanent: true);
    }
    
    debugPrint('Repository层初始化完成');
  }
  
  /// 初始化业务服务层
  static Future<void> _initBusinessServices() async {
    // 用户服务
    if (!Get.isRegistered<UserService>()) {
      final userService = UserService();
      Get.put<UserService>(userService, permanent: true);
    }
    
    // 产品数据缓存服务
    if (!Get.isRegistered<ProductService>()) {
      final productService = ProductService();
      Get.put<ProductService>(productService, permanent: true);
    }
    
    // 设备数据缓存服务
    if (!Get.isRegistered<DeviceService>()) {
      final deviceService = DeviceService();
      Get.put<DeviceService>(deviceService, permanent: true);
    }
    
    // 告警日志数据缓存服务
    if (!Get.isRegistered<AlertLogService>()) {
      final alertLogService = AlertLogService();
      Get.put<AlertLogService>(alertLogService, permanent: true);
    }
    
    // 国标视频服务
    if (!Get.isRegistered<GBVideoService>()) {
      try {
        final gbVideoService = await GBVideoService().init();
        Get.put<GBVideoService>(gbVideoService, permanent: true);
        debugPrint('国标视频服务初始化成功');
      } catch (e) {
        debugPrint('国标视频服务初始化失败: $e');
        // 创建一个未初始化的服务实例，以避免空引用错误
        final fallbackService = GBVideoService();
        Get.put<GBVideoService>(fallbackService, permanent: true);
        debugPrint('已注册备用国标视频服务');
      }
    }
    
    debugPrint('业务服务层初始化完成');
  }
} 